package com.example.dboperate.utils;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * @author scout.du
 * @date 2025/07/08 13:26
 **/
public class ModelGenerateUtils {

    public static void generate(String url,String username,String password,String moduleName, String... tableNamesInclude){
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
//        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir("d:/codeGen");
        gc.setAuthor("scout.du");
        gc.setOpen(false);
        //默认不覆盖，如果文件存在，将不会再生成，配置true就是覆盖
        gc.setFileOverride(true);
        gc.setSwagger2(true); // 实体属性 Swagger2 注解
        //gc.setServiceName("%sService");//去掉Service接口的首字母I
        gc.setBaseColumnList(true);//生成xml文档中的BaseColumnList
        gc.setBaseResultMap(true);//生成xml文档中的ResultMap
        gc.setIdType(IdType.ASSIGN_UUID);
        //设置时间对应类 DateType.ONLY_DATE-->Date  DateType.TIME_PACK-->LocalDateTime  DateType.SQL_PACK-->Timestamp，默认是LocalDateTime
        gc.setDateType(DateType.TIME_PACK);


        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(url);
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername(username);
        dsc.setPassword(password);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(moduleName);
        pc.setParent("com");
        mpg.setPackageInfo(pc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude(tableNamesInclude);
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setTablePrefix("sys_");

        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setEntityColumnConstant(true);

//        strategy.setSuperEntityClass("com.baomidou.mybatisplus.samples.generator.common.BaseEntity");
//        strategy.setSuperControllerClass("com.baomidou.mybatisplus.samples.generator.common.BaseController");
//        strategy.setSuperEntityColumns("id");
        strategy.setControllerMappingHyphenStyle(true);
//        strategy.entityTableFieldAnnotationEnable(true);
        mpg.setStrategy(strategy);
        // 选择 freemarker 引擎需要指定如下加，注意 pom 依赖必须有！
        //mpg.setTemplateEngine(new FreemarkerTemplateEngine());

        //configCustomizedCodeTemplate(mpg);
        //configInjection(mpg);

        mpg.execute();
    }
}
